// pages/my/my-wallet/my-wallet.js
var request = require('../../../utils/request.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curId: 100,
    btnIsEnable: "",
    isDeposit: 10003
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getToken();
  },
  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        var tempToken = res.data;
        var postData = {};
        postData.session_id = tempToken;
        console.log(postData)
  
        //getIsDeposit 判断是否交过押金
        request.getIsDeposit(
          postData,
          (res) => {
            console.log(res);
            if (res.data.code != "10001") {
              that.setData({
                isDeposit: res.data.code
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                image: "../../../images/xx.png"
              })
            }
          },
        )
        request.getTotalMoney(
          postData,
          (res) => {
            console.log(res);
            that.setData({
              totalMoney: res.data
            })
          },
        )

      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },

  onShow: function () {
    var that = this;
    that.getToken();

  },
  //
  goDealDetail: function () {
    wx.navigateTo({
      url: '../my-dealDetail/my-dealDetail'
    })
  },
  //
  chooseMoney: function (e) {
    // console.log(e.target.dataset.id)
    var that = this;
    that.setData({
      curId: e.target.dataset.id,
      btnIsEnable: ''
    })
  },
  goAddMoney: function () {
    var that = this;
    that.setData({
      btnIsEnable: 'disabled'
    })
    var postData = {};
    postData.session_id = that.data.token;
    postData.money = that.data.curId;
    wx.showLoading();
    console.log(postData)
    request.rechargeMoney(
      postData,
      (res) => {
        console.log(res);
        if (res.data.status == "success") {
          wx.hideLoading();
          wx.navigateTo({
            url: '../my-wallet/my-addMoney/my-addMoney?order_no=' + res.data.order_no + "&money=" + that.data.curId,
          })
          // var postData = {};
          // postData.session_id = that.data.token;
          // postData.order_no = res.data.order_no;
          // console.log(postData)
          // request.rechargePay(
          //   postData,
          //   (res) => {
          //     console.log(res);
          //     wx.hideLoading()
          //     if (res.data.status == "error") {
          //       wx.showToast({
          //         title: res.data.msg,
          //         image: "../../../images/xx.png",
          //         success:function(){
          //           that.setData({
          //             btnIsEnable: ''
          //           })
          //         }
          //       })
          //     } else {
          //       wx.requestPayment({
          //         'timeStamp': res.data.timeStamp,
          //         'nonceStr': res.data.nonceStr,
          //         'package': res.data.package,
          //         'signType': 'MD5',
          //         'paySign': res.data.paySign,
          //         'success': function (res) {
          //           console.log(res);
          //           wx.showModal({
          //             title: '',
          //             content: '支付成功',
          //             showCancel: false,
          //             confirmText: "返回",
          //             confirmColor: "#FF0000",
          //             success: function (res) {
          //               that.setData({
          //                 btnIsEnable: ''
          //               })
          //             }
          //           })
          //         },
          //         'fail': function (res) {
          //           console.log(res)
          //           wx.showModal({
          //             title: '支付失败',
          //             content: '请重新支付',
          //             showCancel: false,
          //             confirmText: "我知道了",
          //             confirmColor: "#FF0000",
          //             success:function(){
          //               that.setData({
          //                 btnIsEnable: ''
          //               })
          //             }
          //           })
          //         }
          //       })
          //     }

          //   },
          // )

        }

      },
    )

  },
  addYaJin: function () {
    var that = this;
    var postData = {};
    postData.session_id = that.data.token;
    postData.money = 99;
    wx.showLoading()
    console.log(postData)
    request.addUserDeposit(
      postData,
      (res) => {
        console.log(res);
        console.log(res.data.order_no);
        if (res.data.code != "10001") {
          if (res.data.code == "1" || res.data.code == "2") {
            wx.navigateTo({
              url: '../my-wallet/my-addDeposit/my-addDeposit?order_no=' + res.data.order_no,
            })
          } else if (res.data.code == "10002") {
            wx.showToast({
              title: res.data.msg,
              icon: "success"
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            image: "../../../images/xx.png"
          })
        }
      },
    )
  },


  returnYaJin: function () {
    var that = this;
    wx.showModal({
      title: '退押金',
      content: '确定要退回押金吗？退回押金之后，您将不能使用启阅读吧，无法享受优质的阅读服务。',
      confirmText: "不退了",
      confirmColor: "#FF0000",
      cancelText: "退押金",
      success: function (res) {
        if (res.confirm) {
          console.log("不退了")
        } else {
          var postData = {};
          postData.session_id = that.data.token;
          console.log(postData)
          request.rerundMoney(
            postData,
            (res) => {
              console.log(res);
              if (res.data.status == "success") {
                wx.navigateTo({
                  url: '../my-wallet/my-returnDeposit/my-returnDeposit?order_no=' + res.data.order_no
                })
              } else if (res.data.status == "error") {
                wx.showModal({
                  title: '',
                  content: res.data.msg,
                  confirmColor: "#ff0000",
                  showCancel: false
                })
              }

            },
          )
        }
      }
    })



  },
  goPayTip: function () {
    wx.navigateTo({
      url: '../../my/my-payTip/my-payTip'
    })
  },
})